#include<stdio.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/stat.h>
#include<dirent.h>

int main(int argc,char *argv[])
{
	if(strcmp(argv[1],"-r")==0){
		printf("This function is improving.");
	}
	else{
		int fd=open(argv[1],O_RDONLY);
		if(fd<0){
			perror("file no exsit!\n");
			return 0;
		}
		else{
			struct stat *statbuf=(struct stat *)malloc(sizeof(statbuf));
			stat(argv[2],statbuf);
			if(S_ISDIR(statbuf->st_mode)){
				int i,j,k=0;
				for(i=strlen(argv[1])-1;i>=0;--i){
					if(argv[1][i]=='/'){
						break;
					}
				}	
				char source[128]={};
				for(j=i+1;j<strlen(argv[1]);++j){
					source[k++]=argv[1][j];		
				}
				if(argv[2][strlen(argv[2])-1]!='/'){
					strcat(argv[2],"/");
				}
				int fp=open(strcat(argv[2],source),O_WRONLY|O_TRUNC|O_CREAT,S_IREAD|S_IWRITE|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
				while(1){
					char buf[10]={};
					if(read(fd,buf,10)==0){
						break;
					}
					else{
						write(fp,buf,10);
					}	
				}
				close(fp);
			}
			else{
					int fp=open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,S_IREAD|S_IWRITE|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
					while(1){
						char buf[10]={};
						if(read(fd,buf,10)==0){
							break;
						}
						else{
							write(fp,buf,10);
						}	
					}
					close(fp);	
			}
		}
		close(fd);
	}
	return 0;
}